import { LocalFileParserFactory } from "./localfile/LocalFileParserFactory";
var LocalFileSvc = {}
LocalFileSvc.open = function () {
    console.log('openfile***************');
    const accept = '.lgx,.dxf,.svg,.pat';
    const formatArr = ['application/lgx', 'dxf/dxf', 'svg/svg', 'cad pat/pat'];
    this.selectFile(accept, formatArr).then(res => {
        const file = res.file;
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
            let imageBase64 = e.target.result;
            const base64String = imageBase64.split(',')[1]; // 将dataURL头部分割掉
            const fileName = file.name;
            const fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
            let svc = LocalFileParserFactory.getParser(fileExtension);
            if (svc) {
                svc.parse(base64String, window.mainctl);
            }
        };
    })
        .catch((e) => {
            window.$message.error('仅支持' + accept + '格式的图片')
        })
}



LocalFileSvc.selectFile = function (accept, formatArr) {
    return new Promise((resolve, reject) => {
        const fileInput = document.createElement('input')
        fileInput.type = 'file';
        fileInput.accept = accept;
        fileInput.click();
        fileInput.onchange = function (event) {
            const file = event.target.files[0]
            {
                resolve({ message: true, event, file })
            }
        }
    });
}

LocalFileSvc.openLocalMemFileData = function (localFileInfo) {
    let fileName = localFileInfo.fileName;
    const fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
    let svc = LocalFileParserFactory.getParser(fileExtension);
    if (svc) {
        svc.parse(localFileInfo.data, window.mainctl);
    }
}

export { LocalFileSvc }